package com.soonfor.evaluate.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.view.RoundImageView;
import com.soonfor.evaluate.tools.ImageUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import com.estronger.shopmanage.R;
import com.soonfor.evaluate.bean.EvaluateRankingBean;

/**
 * 作者：DC-DingYG on 2018-10-17 13:57
 * 邮箱：dingyg012655@126.com
 */
public class EvaluateRankView extends LinearLayout {
    private Context mContext;
    @BindView(R.id.left_avatar)
    RoundImageView leftAvatar;
    @BindView(R.id.tv_left_ranking)
    TextView tvLeftRanking;
    @BindView(R.id.tvfLeftGoodRate)
    TextView tvfLeftGoodRate;
    @BindView(R.id.tvfLeftGrade)
    TextView tvfLeftGrade;
    @BindView(R.id.me_avatar)
    RoundImageView middleAvatar;
    @BindView(R.id.tv_me_ranking)
    TextView tvMiddleRanking;
    @BindView(R.id.tvfMiddleGoodRate)
    TextView tvfMiddleGoodRate;
    @BindView(R.id.tvfMiddleGrade)
    TextView tvfMiddleGrade;
    @BindView(R.id.right_avatar)
    RoundImageView rightAvatar;
    @BindView(R.id.tv_right_ranking)
    TextView tvRightRanking;
    @BindView(R.id.tvfRightGoodRate)
    TextView tvfRightGoodRate;
    @BindView(R.id.tvfRightGrade)
    TextView tvfRightGrade;
    @BindView(R.id.rl_rank1)
    RelativeLayout rlRank1;
    @BindView(R.id.rl_rank2)
    RelativeLayout rlRank2;
    @BindView(R.id.rl_rank3)
    RelativeLayout rlRank3;
//    @BindView(R.id.leftKuan)
//    ImageView leftKuan;
//    @BindView(R.id.middleKuan)
//    ImageView middleKuan;
//    @BindView(R.id.rightKuan)
//    ImageView rightKuan;

    @BindView(R.id.tvfLeftName)
    TextView tvfLeftName;
    @BindView(R.id.tvfMiddleName)
    TextView tvfMiddleName;
    @BindView(R.id.tvfRightName)
    TextView tvfRightName;

    public EvaluateRankView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.view_evaluate_ranking, this);
        ButterKnife.bind(this, view);
    }
    //初始化
    public void initEvaluateRankingView(final Activity mContext, final List<EvaluateRankingBean> list, final int position, final SetMainView smView) {
        this.mContext = mContext;
        if (list != null && list.size() > 0) {
            rlRank2.setVisibility(View.VISIBLE);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mContext.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if(list.size()==1){
                                    rlRank2.setVisibility(VISIBLE);
                                    rlRank1.setVisibility(INVISIBLE);
                                    rlRank3.setVisibility(INVISIBLE);
                                    tvMiddleRanking.setText(getRanksort(list, 0));
                                    tvfMiddleName.setText(list.get(0).getFusername());
                                    tvfMiddleGoodRate.setText(list.get(0).getFallhighappraiserate() + "%");
                                    tvfMiddleGrade.setText(list.get(0).getFavgrtnvisitpoint() + "分");
                                    ImageUtils.loadImage(mContext, list.get(0).getFheadpic(), middleAvatar, R.drawable.user2);
                                }else if(list.size()==2){
                                    rlRank1.setVisibility(VISIBLE);
                                    rlRank2.setVisibility(VISIBLE);
                                    rlRank3.setVisibility(INVISIBLE);
                                    tvLeftRanking.setText(getRanksort(list, 1));
                                    tvfLeftName.setText(list.get(1).getFusername());
                                    tvfLeftGoodRate.setText(list.get(1).getFallhighappraiserate() + "%");
                                    tvfLeftGrade.setText(list.get(1).getFavgrtnvisitpoint() + "分");
                                    tvMiddleRanking.setText(getRanksort(list, 0));
                                    tvfMiddleName.setText(list.get(1).getFusername());
                                    tvfMiddleGoodRate.setText(list.get(0).getFallhighappraiserate() + "%");
                                    tvfMiddleGrade.setText(list.get(0).getFavgrtnvisitpoint() + "分");
                                    ImageUtils.loadImage(mContext, list.get(1).getFheadpic(), leftAvatar, R.drawable.user2);
                                    ImageUtils.loadImage(mContext, list.get(0).getFheadpic(), middleAvatar, R.drawable.user2);
                                }else if(list.size() >= 3){
                                    rlRank1.setVisibility(VISIBLE);
                                    rlRank2.setVisibility(VISIBLE);
                                    rlRank3.setVisibility(VISIBLE);
                                    tvLeftRanking.setText(getRanksort(list, 1));
                                    tvfLeftName.setText(list.get(1).getFusername());
                                    tvfLeftGoodRate.setText(list.get(1).getFallhighappraiserate() + "%");
                                    tvfLeftGrade.setText(list.get(1).getFavgrtnvisitpoint() + "分");
                                    tvMiddleRanking.setText(getRanksort(list, 0));
                                    tvfMiddleName.setText(list.get(0).getFusername());
                                    tvfMiddleGoodRate.setText(list.get(0).getFallhighappraiserate() + "%");
                                    tvfMiddleGrade.setText(list.get(0).getFavgrtnvisitpoint() + "分");
                                    tvRightRanking.setText(getRanksort(list, 2));
                                    tvfRightName.setText(list.get(2).getFusername());
                                    tvfRightGoodRate.setText(list.get(2).getFallhighappraiserate() + "%");
                                    tvfRightGrade.setText(list.get(2).getFavgrtnvisitpoint() + "分");
                                    ImageUtils.loadImage(mContext, list.get(1).getFheadpic(), leftAvatar, R.drawable.user2);
                                    ImageUtils.loadImage(mContext, list.get(0).getFheadpic(), middleAvatar, R.drawable.user2);
                                    ImageUtils.loadImage(mContext, list.get(2).getFheadpic(), rightAvatar, R.drawable.user2);
                                }
                            }catch (Exception e){}
                        }
                    });
                }
            }).start();
            if(position>=0){
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mContext.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    if(smView!=null) {
                                        smView.callback(list.get(position));
                                    }
                                }catch (Exception e){}
                            }
                        });
                    }
                }).start();
            }
        }
    }

    private String getRanksort(List<EvaluateRankingBean> list, int position){
        if(!list.get(position).getFsno().equals("")){
            return "第" + list.get(position).getFsno() + "名";
        }else
            return "";
    };

    public interface SetMainView{
        void callback(EvaluateRankingBean currtBean);
    }
}
